<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      [v-cloak] {
        display: none;
      }

      .box1,
      .box2 {
        width: 200px;
        height: 200px;
        background-color: lightblue;
      }

      .box2 {
        background-color: orange;
      }

      .v-enter,
      .v-leave-to {
        opacity: 0;
      }

      .v-enter-active,
      .v-leave-active {
        transition: all 2s;
      }
      .v-enter-to,
      .v-leave {
        opacity: 1;
      }
    </style>
  </head>
  <body>
    <!-- 多元素切换的动画效果注意点： -->
    <!-- 1.  不同的元素上应该有唯一的 key， vue是根据key来区分它们的 -->
    <!-- 2.  添加mode 来决定是先进后出in-out  还是先出后进out-in -->
    <!-- in-out表示一个先出去，另一个再进来 -->
    <div id="app" v-cloak>
      <button @click="flag=!flag">点击</button>
      <transition mode="in-out">
        <div class="box1" v-if="flag" key="box1"></div>
        <div class="box2" v-else key="box2"></div>
      </transition>
    </div>
    <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.9/vue.js"></script>
    <script>
      var vm = new Vue({
        el: "#app",
        data: {
          flag: true,
        },
      });
    </script>
  </body>
</html>